package com.lgl.scsheader.system.controller;

import com.lgl.scsapi.system.model.PageData;
import com.lgl.scsapi.util.utils.Tools;
import com.lgl.scsheader.base.controller.BaseController;
import com.lgl.scsheader.system.service.DictionariesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/** 
 * 字典Controller类
 */
@RestController
@RequestMapping(value="/dictionaries")
public class DictionariesController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(DictionariesController.class);
	@Resource(name="dictionariesService")
	private DictionariesService dictionariesService;

	//保存
	@RequestMapping(value="/save")
	public ModelAndView save(PrintWriter out) throws Exception{
		//ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		PageData pdp = new PageData();
		pdp = this.getPageData();
		
		String PARENT_ID = pd.getString("PARENT_ID");
		pdp.put("ZD_ID", PARENT_ID);
		
		if(null == pd.getString("ZD_ID") || "".equals(pd.getString("ZD_ID"))){
			if(null != PARENT_ID && "0".equals(PARENT_ID)){
				pd.put("JB", 1);
				pd.put("P_BM", pd.getString("BIANMA"));
			}else{
				pdp = dictionariesService.findById(pdp);
				pd.put("JB", Integer.parseInt(pdp.get("JB").toString()) + 1);
				pd.put("P_BM", pdp.getString("BIANMA") + "_" + pd.getString("BIANMA"));
			}
			pd.put("ZD_ID", Tools.getUUID());	//ID
			dictionariesService.save(pd);
		}else{
			pdp = dictionariesService.findById(pdp);
			if(null != PARENT_ID && "0".equals(PARENT_ID)){
				pd.put("P_BM",  pd.getString("BIANMA"));
			}else{
				pd.put("P_BM", pdp.getString("BIANMA") + "_" + pd.getString("BIANMA"));
			}
			
			dictionariesService.edit(pd);
		}
		
		/*mv.addObject("msg","success");
		mv.setViewName("save_result");*/
		return null;
	}

	/**
	 * 获取所有字典数据
	 * @return 字典数据集合
	 */
	@RequestMapping(value = "listDictionaries")
	public Object listDictionaries(){
		PageData dicPd = this.getPageData();
		return dictionariesService.listDictionaries(dicPd);
	}
	
	//递归
	/*public void getZDname(String PARENT_ID) {
		try {
			PageData pdps = new PageData();;
			pdps.put("ZD_ID", PARENT_ID);
			pdps = dictionariesService.findById(pdps);
			if(pdps != null){
				szdList.add(pdps);
				String PARENT_IDs = pdps.getString("PARENT_ID");
				this.getZDname(PARENT_IDs);
			}
		} catch (Exception e) {
			logger.error(e.toString(),e);
		}
	}*/

	/**
	 * 判断编码是否存在
	 */
	@RequestMapping(value="/has")
	public void has(PrintWriter out){
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			if(dictionariesService.findBmCount(pd) != null){
				out.write("error");
			}else{
				out.write("success");
			}
			out.close();
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
	}
	
	// 删除用户
	@RequestMapping(value="/del")
	public Object del(){
		Map<String,String> map = new HashMap<String,String>();
		PageData pd = new PageData();
		String errInfo = "";
		try{
			pd = this.getPageData();
			
			if(Integer.parseInt(dictionariesService.findCount(pd).get("ZS").toString()) != 0){
				errInfo = "false";
			}else{
				dictionariesService.delete(pd);
				errInfo = "success";
			}
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		
		map.put("result", errInfo);
		return map;
	}
	
}